package com.shop2cn.iapi.coupon.testcase.old;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponusecondition;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponuseconditionWapper;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.model.couponbatch;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.shop2cn.iapi.coupon.parameter.GetBatchInfosBean;
import com.shop2cn.iapi.coupon.parameter.batchInfo;
import com.shop2cn.iapi.coupon.parameter.batchInfos;
import com.shop2cn.iapi.coupon.service.GetBatchInfosCall;

@TestSuite
public class Ts_GetBatchInfos {
	private static GetBatchInfosBean GetBatchInfosBean;
    private static GetBatchInfosCall GetBatchInfosCall;
    private static CreateCouponBean createCouponBean=new CreateCouponBean();

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取批次信息请求信息用例");
    }

    @Before
    public void caseUp() {
        GetBatchInfosBean = new GetBatchInfosBean();
        GetBatchInfosCall = new GetBatchInfosCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(GetBatchInfosCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_001() {
        Logger.start(true, "获取批次信息请求信息用例-单条");
        try {        	
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(list.get(0).get("BatchCode").toString());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_002() {
        Logger.start(true, "获取批次信息请求信息用例-多条");
        try {
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();

            for (Map batchCode : list) {
                batchCodes.add(batchCode.get("BatchCode").toString());
            }

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_003() {
        Logger.start(true, "获取批次信息请求信息用例-单条商家优惠券批次-正常状态");
        try {
        	CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();

            List<String> batchCodes = new ArrayList<String>();
          
			batchCodes.add(batchCode);

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            batchInfos batchInfos = GetBatchInfosCall.getData();
            batchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");

            Logger.verifyEquals(0, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_004() {
        Logger.start(true, "获取批次信息请求信息用例-单条-作废");
        try {
        	CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			
            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            // 作废优惠券
            couponCallService.InvalidSellerBatch(couponBatchBean.getSellerId(),batchCode);

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            batchInfos batchInfos = GetBatchInfosCall.getData();   
            batchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
            // 验证状态为作废：1
            Logger.verifyEquals(1, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }


    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_006() {
        Logger.start(true, "获取批次信息请求信息用例-单条-领完");
        try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			
            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            // 设置优惠券领完
            couponbatch batch = new couponbatchWapper().selectBybatchCode(batchCode).get(0);
            new couponbatchWapper().setiReceiveCount(batch.getBatchId(), batch.getTotalNum());
            
//            int iCouponSettingId = batch.getiCouponSettingId();
//            new YmtCouponSettingWapper().setiReceiveCount(iCouponSettingId, batch.getiCouponTotalNum());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            batchInfos batchInfos = GetBatchInfosCall.getData();
            batchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
            // 验证状态为过期：3
            Logger.verifyEquals(3, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_008() {
        Logger.start(true, "获取批次不存在");
        try {        	
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add("999999999");

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            List<batchInfo> batchInfos = GetBatchInfosCall.getData().getBatchInfos();

            Logger.verifyEquals(batchInfos.size(), 0, "验证返回的长度与请求的batchCodes长度一致");
            
          //  verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_009() {
        Logger.start(true, "获取批次信息请求信息用例-活动优惠券-JIRACONN-23166");
        try {        	
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatch_ActivityBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(list.get(0).get("BatchCode").toString());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_010() {
        Logger.start(true, "获取批次信息请求信息用例-品牌优惠券-JIRACONN-23166");
        try {        	
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
        	 createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
        	
        	List<Integer> brands = new ArrayList<Integer>();
        	brands.add(1);
        	brands.add(2);
        	createCouponBean.setBrands(brands);
    		
        	CreateCouponCall createCouponCall = CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
			
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(list.get(0).get("BatchCode").toString());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_011() {
        Logger.start(true, "获取批次信息请求信息用例-单条-相对时间是小时");
        try {        	
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
       	 createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
       	createCouponBean.setEffectiveType(1);
       	createCouponBean.setEffectiveTimeType(1);
       	createCouponBean.setEffectiveDays(3);
   		
       	CreateCouponCall createCouponCall = CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
			
           List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

           List<String> batchCodes = new ArrayList<String>();
           batchCodes.add(list.get(0).get("BatchCode").toString());

           GetBatchInfosBean.setBatchCodes(batchCodes);

           GetBatchInfosCall.setData(GetBatchInfosBean);
           GetBatchInfosCall.callService();

           Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
           Logger.verifyEquals("操作成功！",
                   GetBatchInfosCall.getMessage(), "验证返回Message");

           verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_012() {
        Logger.start(true, "获取批次信息请求信息用例-团长券指定商品-JIRACONN-26644");
        try {        	
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
        	 createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
        	 createCouponBean.setIsLimitGroupSponsor(true);
        	List<String> productIds = new ArrayList<String>();
        	productIds.add("1111");
        	productIds.add("2222");
        	createCouponBean.setProductIds(productIds);
        	CreateCouponCall createCouponCall = CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
			
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(list.get(0).get("BatchCode").toString());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_013() {
        Logger.start(true, "获取批次信息请求信息用例-团长券不指定商品-JIRACONN-26644");
        try {        	
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
        	 createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
        	 createCouponBean.setIsLimitGroupSponsor(true);
//        	List<String> productIds = new ArrayList<String>();
//        	productIds.add("1111");
//        	productIds.add("2222");
//        	createCouponBean.setProductIds(productIds);
        	CreateCouponCall createCouponCall = CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
			
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(list.get(0).get("BatchCode").toString());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");
            verifyBatchInfos();
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_014() {
        Logger.start(true, "获取批次信息请求信息用例-团长券指定商品 与普通平台指定商品 混合-JIRACONN-26644");
        try {        	
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
        	 createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
         	CreateCouponCall createCouponCall = CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
        	 createCouponBean.setIsLimitGroupSponsor(true);
        	List<String> productIds = new ArrayList<String>();
        	productIds.add("1111");
        	productIds.add("2222");
        	createCouponBean.setProductIds(productIds);
        	CreateCouponCall createCouponCall2 = CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(createCouponCall.getBatchCode().toString());
            batchCodes.add(createCouponCall2.getBatchCode().toString());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");
            verifyBatchInfos();
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    private void verifyBatchInfos()
    {
        List<batchInfo> batchInfos = GetBatchInfosCall.getData().getBatchInfos();
        
        List<String> batchCodes = GetBatchInfosBean.getBatchCodes();

        Logger.verifyEquals(batchInfos.size(), batchCodes.size(), "验证返回的长度与请求的batchCodes长度一致");
        int num = batchInfos.size();
        for (int i = 0; i <batchInfos.size() ; i++) {
        	
        	batchInfo batchInfo = batchInfos.get(i);
            couponbatch batch = new couponbatchWapper().selectBybatchCode(batchInfo.getBatchCode()).get(0);

            Logger.verifyEquals(true,batchCodes.contains(batchInfo.getBatchCode()), "验证第"+(i+1)+"条请求batchCodes包含当前batchCode");
            Logger.verifyEquals(batch.getBatchId(), batchInfo.getBatchId(), "验证第"+(i+1)+"条返回BatchId为正常");
            Logger.verifyEquals(batch.getCreaterType().intValue(), batchInfo.getCreaterType(), "验证第"+(i+1)+"条返回CreaterType为正常");
            Logger.verifyEquals(batch.getReceiveTimesPerUser().intValue(), batchInfo.getMaxReceiveCountPerUser(), "验证第"+(i+1)+"条返回maxReceiveCountPerUser为正常");
            
            Logger.verifyEquals(getDateString(batch.getValidStart()), batchInfo.getValidStart(), "验证第"+(i+1)+"条返回ValidStart为正常");
            Logger.verifyEquals(getDateString(batch.getValidEnd()), batchInfo.getValidEnd(), "验证第"+(i+1)+"条返回ValidEnd为正常");
            
            Logger.verifyEquals(batch.getCouponType(), batchInfo.getCouponType(), "验证第"+(i+1)+"条返回CouponType为正常");
            Logger.verifyEquals(batch.getCouponTypeBizValue(), batchInfo.getCouponTypeBizValue(), "验证第"+(i+1)+"条返回CouponTypeBizValue为正常");
            Logger.verifyEquals(batch.getCouponTypeBizValue(), batchInfo.getActivityIds(), "验证第"+(i+1)+"条返回ActivityIds为正常");
            Logger.verifyEquals(getDateString(batch.getAcquireStartTime()), batchInfo.getAcquireStartTime(), "验证第"+(i+1)+"条返回AcquireStartTime为正常");
            Logger.verifyEquals(getDateString(batch.getAcquireEndTime()), batchInfo.getAcquireEndTime(), "验证第"+(i+1)+"条返回AcquireEndTime为正常");
            
            Logger.verifyEquals(batch.getEffectiveTimeType(), batchInfo.getEffectiveTimeType(), "验证第"+(i+1)+"条返回EffectiveTimeType为正常");
            
            
            if(createCouponBean!=null && createCouponBean.getBrands()!=null && createCouponBean.getBrands().size()>1)
            {
            	List<couponusecondition> lst=new couponuseconditionWapper().selectByBatchId(batch.getBatchId());
            	
            	Logger.verifyEquals(createCouponBean.getBrands().size(), lst.size(), "验证第"+(i+1)+"条返回的长度");
            	
            	String str="";
            	for(couponusecondition c:lst)
            	{
            		str+=c.getConditionValue()+",";
            		 Logger.verifyEquals(6,c.getConditionType(),"验证第"+(i+1)+"条返回ConditionType为正常");
            	}
            	 Logger.verifyEquals(str.subSequence(0, str.length()-1), batchInfo.getBrandIds(), "验证第"+(i+1)+"条返回BatchId为正常");
            }
            
            if(batch.getUseConditionType()==9)
            {
            	List<couponusecondition> clist=new couponuseconditionWapper().selectByBatchId(batch.getBatchId());
            	for(int j=0;j<clist.size();j++)
            	{
            		Logger.verifyEquals(2,clist.get(j).getConditionType(), "验证第"+(j+1)+"条返回ConditionType为正常");
            		Logger.verifyEquals(clist.get(j).getConditionValue(), batchInfo.getUsableProductIds().get(j), "验证第"+(j+1)+"条返回UsableProductIds为正常");
            	}
            }
            else
            	  Logger.verifyEquals(0, batchInfo.getUsableProductIds().size(), "验证第"+(i+1)+"条返回UsableProductIds为正常");
            
        }
    }
    
    private void verifyStatus(int BatchStatusInfo)
    {
        List<batchInfo> batchInfos = GetBatchInfosCall.getData().getBatchInfos();

        for (int i = 0; i < batchInfos.size(); i++) {
            batchInfo batchInfo = batchInfos.get(i);
            Logger.verifyEquals(BatchStatusInfo, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
        }
    }
    
    
    private String getDateString(Date date) {
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");

        return formatter1.format(date) + "T" + formatter2.format(date);
    }
    
    
}
